<template>
  <div>
    <el-dialog :visible.sync="dialogVisible" v-if="dialogVisible" width="60%">
      <el-form :model="formData" :rules="rules" ref="dataFrom" label-width="150px">
        <!--        <el-form-item label="商品id" prop="id">-->
        <!--          <el-input placeholder="如需要更新数据请输入对应id，如需新增数据则不输入此项" v-model="formData.id"></el-input>-->
        <!--        </el-form-item>-->
        <el-form-item label="省" prop="province">
          <el-input v-model="formData.province"></el-input>
        </el-form-item>
        <el-form-item label="市" prop="city">
          <el-input v-model="formData.city"></el-input>
        </el-form-item>
        <el-form-item label="区域" prop="area">
          <el-input v-model="formData.area"></el-input>
        </el-form-item>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="formData.address"></el-input>
        </el-form-item>
        <el-form-item label="电话" prop="telephone">
          <el-input v-model="formData.telephone"></el-input>
        </el-form-item>
        <el-form-item label="顾客id" prop="customerId">
          <el-input v-model="formData.customerId"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="uploadData">立即上传</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import {uuid as $uuid} from "vue-uuid";
import qs from "qs";
import axios from 'axios';

export default {
  name: "addaddress",
  data() {
    return{
      fileList:[],
      filename:'',
      imageUrl:'',
      formData:{
        id:'',
        province:"",
        city:"",
        area:"",
        address:"",
        telephone:"",
        customerId:""
      },
      rules:{
      },
      dialogVisible: false,
      infoData:[],
      showAll:false
    }
  },
  methods:{
    uploadData(){
      this.$refs.dataFrom.validate((valid) => {
        if (valid) {
          let that=this;
          let url=that.$URL.addaddress;
          console.log("filelist")
          console.log(that.fileList)

          let uploadaddressParams = qs.stringify({
            id:"",
            province:that.formData.province,
            city:that.formData.city,
            area:that.formData.area,
            address:that.formData.address,
            telephone:that.formData.telephone,
            customerId:that.formData.customerId
          }, { indices: false });
          console.log(uploadaddressParams)

          console.log(url)
          debugger;
          let y = "http://39.96.93.100:6677/address/saveOrUpdate"
          that.$axios.post(that.$URL.addaddress, uploadaddressParams).then(
              res => {
                if (res.status == 200) {
                  that.$message({
                    message: "上传成功",
                    type: 'success'
                  });
                  //that.$refs.formData.resetFields();
                } else {
                  that.$message({
                    message: "上传失败",
                    type: 'error'
                  });
                }
              }
          );
        }
      });
    },

    appendFile(file)
    {
      this.filename = file.file
      console.log("filename"+file.file);
      console.log(this.fileList)
      //this.formData.append('file',file.file);
    },

    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    handleExceed(files, fileList) {
      console.log(fileList)
      this.$message.warning(`当前限制选择 1 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${ file.name }？`);
    },

    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      console.log(this.imageUrl)
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 5;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      this.imageUrl = file;
      console.log(file)
      console.log("before"+this.imageUrl.file)
      return isJPG && isLt2M;
    }
  },
  created() {
    let that=this;
    this.$Bus.$on('addaddress',param=>{
      that.dialogVisible=param.dialogVisible
      that.infoData=param.infoData
      that.showAll=false
    })
  }
}
</script>

<style scoped>

</style>
